package com.chenfy.touchtransfer_android.tmp;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.companion.CompanionDeviceManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.chenfy.ioc_annotation.BindLayout;
import com.chenfy.ioc_annotation.BindView;
import com.chenfy.touchtransfer_android.R;
import com.chenfy.touchtransfer_android.base.BaseActivity;


import java.util.Collection;

/**
 * Created by ChenFengyao
 * Date: 20-11-10
 */
@BindLayout(R.layout.activity_wifi)
public class WIFIActivity extends BaseActivity implements Wifip2pActionListener {

    private WifiP2pManager mWifiP2pManager;
    private WifiP2pManager.Channel mChannel;
    private Wifip2pReceiver mWifip2pReceiver;
    private WifiP2pInfo mWifiP2pInfo;

    @BindView(value = R.id.startServer, click = true)
    public Button mStartBtn;
    @BindView(value = R.id.stopServer, click = true)
    public Button mStopBtn;

    @Override
    protected void initData(@Nullable Bundle savedInstanceState, Intent preIntent) {
        super.initData(savedInstanceState, preIntent);

        mWifiP2pManager = (WifiP2pManager) getSystemService(WIFI_P2P_SERVICE);
        mChannel = mWifiP2pManager.initialize(this, getMainLooper(), this);

        // 注册广播
        mWifip2pReceiver = new Wifip2pReceiver(mWifiP2pManager, mChannel, this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION);
        registerReceiver(mWifip2pReceiver, intentFilter);


        BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
        String deviceName = myDevice.getName();
        Toast.makeText(this, deviceName, Toast.LENGTH_SHORT).show();
        Toast.makeText(this, Build.MODEL, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.startServer:
                Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    Log.d("WIFIActivity", "没给定位权限");
                    return;
                }



                // 搜索设备
                mWifiP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        Log.d("WIFIActivity", "discoverPeers Success");
                    }

                    @Override
                    public void onFailure(int reason) {
                        Log.d("WIFIActivity", "discoverPeers Failure");
                    }
                });
                break;
            case R.id.stopServer:
                Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
                mWifiP2pManager.stopPeerDiscovery(mChannel, null);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unregisterReceiver(mWifip2pReceiver);
    }

    @Override
    public void wifiP2pEnabled(boolean enabled) {

    }

    @Override
    public void onConnection(WifiP2pInfo wifiP2pInfo) {

    }

    @Override
    public void onDisconnection() {

    }

    @Override
    public void onDeviceInfo(WifiP2pDevice wifiP2pDevice) {

    }

    @Override
    public void onPeersInfo(Collection<WifiP2pDevice> wifiP2pDeviceList) {

    }

    @Override
    public void onChannelDisconnected() {

    }
}
